<template>
  <div>
    <el-card>
      <div slot="header">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-form :model="articleForm" label-width="60px">
        <el-form-item label="状态：">
          <el-radio-group v-model="articleForm.status">
            <el-radio :label="null">全部</el-radio>
            <el-radio label="0">草稿</el-radio>
            <el-radio label="1">待审核</el-radio>
            <el-radio label="2">审核通过</el-radio>
            <el-radio label="3">审核失败</el-radio>
            <el-radio label="4">已删除</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="频道：">
          <el-select v-model="articleForm.channel_id" placeholder="请选择频道">
            <el-option label="全部" :value="null"></el-option>
            <el-option :label="item.name" :value="item.id" v-for="  item in channelList " :key="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label=" 日期：">
          <el-date-picker v-model="randerDate" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期" format='yyyy-MM-dd' value-format='yyyy-MM-dd'>
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit" type="primary">筛选</el-button>
        </el-form-item>
      </el-form>

    </el-card>
    <el-card>
      <div slot="header">
        根据筛选条件共查询到 {{totalCount}} 条结果：
      </div>
      <el-table v-loading='tableloading' border stripe :data="articleList" style="width: 100%" size="mini">
        <el-table-column label="封面">
          <template slot-scope='scope'>
            <el-image fit='cover' :src="scope.row.cover.images[0]" lazy></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题">
        </el-table-column>
        <el-table-column label="状态">
          <template slot-scope="scope">
            <el-tag size="mini" v-if="scope.row.status===0">草稿</el-tag>
            <el-tag size="mini" type="primary" v-else-if="scope.row.status===1">待审核</el-tag>
            <el-tag size="mini" type="success" v-else-if="scope.row.status===2">审核通过</el-tag>
            <el-tag size="mini" type="warning" v-else-if="scope.row.status===3">审核失败</el-tag>
            <el-tag size="mini" type="danger" v-else>已删除</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button @click="$router.push('/publish?id='+scope.row.id) " size="mini" type="primary" icon="el-icon-edit" circle></el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" circle></el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination background @current-change="handleCurrentChange" :current-page="articleForm.page" :page-size="articleForm.per_page" layout="total, prev, pager, next" :total="totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>
<script>
import { getArticles, getArticleChannels } from '@/api/user.js'
export default {
  data() {
    return {
      articleForm: {
        begin_pubdate: null,
        end_pubdate: null,
        status: null,
        per_page: 20,
        page: 1,
        channel_id: null
      },
      randerDate: null,
      totalCount: 0,
      channelList: [],
      tableloading: false,
      articleList: []
    }
  },
  created() {
    this.loadArticle()
    this.loadChannel()
  },
  methods: {
    loadChannel() {
      getArticleChannels().then(res => {
        this.channelList = res.data.data.channels
      })
    },
    loadArticle() {
      this.tableloading = true
      this.articleForm.begin_pubdate = this.randerDate ? this.randerDate[0] : null
      this.articleForm.end_pubdate = this.randerDate ? this.randerDate[1] : null
      getArticles(this.articleForm)
        .then(res => {
          this.articleList = res.data.data.results
          this.totalCount = res.data.data.total_count
          this.tableloading = false
        })
        .catch(() => {
          this.tableloading = false
        })
    },
    onSubmit() {
      this.articleForm.page = 1
      this.loadArticle()
    },
    handleCurrentChange(page) {
      this.articleForm.page = page
      this.loadArticle()
    }
  }
}
</script>

<style scoped>
.el-card:last-child {
  margin-top: 15px;
}
.el-image {
  width: 100px;
  height: 100px;
}
</style>
